package com.offer.BlankReplace;

public class Solution {

    public int replaceBlank(char[] string, int length) {

        if (string == null || length <= 0) {
            throw new IllegalArgumentException();
        }

        int blankCount = 0;
        for (int i = 0; i < length; i++) {
            if (string[i] == ' ') {
                blankCount++;
            }
        }

        //1. '' --> %20, 每个空格多了两个字符
        int newArrayLength = length + blankCount * 2;

        //2. 
        char[] newString = new char[newArrayLength];

        int index = 0;
        for (int i = 0; i < length; ++i) {
            if (string[i] == ' ') {
                newString[index++] = '%';
                newString[index++] = '2';
                newString[index++] = '0';
            } else {
                newString[index++] = string[i];
            }
        }
        System.out.println(new String(newString));
        return newArrayLength;
    }

    public static void main(String[] args) {
        String s = "Mr John Smith";
        System.out.println(new Solution().replaceBlank(s.toCharArray(), s.length()));
    }
}